﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_AccountPrintForm
 * 类型：       控制层
 * 功能说明：   监控
 * 作者：       郭晓庆
 * 创建日期：   2008/02/01
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Master.Bean;
using Shpm.App.Master.Dao;
using System.Globalization;
using Shpm.App.Commons.Utils;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using System.Text.RegularExpressions;
using Shpm.App.Input.Dao;
using Shpm.App.Input.Bean;
using Shpm.App.Commons.Db.Beans.Master;

public partial class App_Input_AccountPrintForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string printType = Convert.ToString(Request.QueryString["PRINT_TYPE"]);
        string date = Convert.ToString(Request.QueryString["DATE"]);
        string id = Convert.ToString(Request.QueryString["ID"]);
        /*单份打印*/
        if ("ONE".Equals(printType))
        {
            set1(date, id);
        }
        /*全部打印*/
        else
        {
            set2(date, id);
        }
    }
    private void set2(string date, string id)
    {

        /*打印设置*/
        MemoryStream stream = new MemoryStream();


        Document document = new Document(PageSize.A4, 80, 50, 30, 65);



        PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream);
        /*打印开始*/
        document.Open();


        FontFactory.RegisterDirectory("c:\\Windows\\Fonts");

        FontSelector selector = new FontSelector();

        Font font = FontFactory.GetFont("simsun", BaseFont.IDENTITY_H, 16, Font.BOLD, new Color(0, 0, 0));

        selector.AddFont(font);



        Paragraph title1 = new Paragraph(selector.Process("亲和源养老社区一卡通对账单"));


        title1.Alignment = Element.ALIGN_CENTER;

        document.Add(title1);

        string str = null;
        double lastCount = 0.00;
        double count = 0.00;
        bool payFlg = false;
        AccountDao dao = new AccountDao();
        MemberReceivableBean memberReceivableBean =null;
        MemberReceivableDetailBean bean = new MemberReceivableDetailBean();
        MemberReceivableDetailBean paramBean = null;
        IList listId = new ArrayList();
        string[] strId = id.Split(',');
        for (int i = 0; i < strId.Length; i++)
        {
            listId.Add(strId[i]);
        }
        bean.ListReceivableId = listId;
        IList list = dao.FindAccountsPrint(bean);

        if (list.Count <= 0)
            return;

        /*打印格式设置*/
        float[] widths = { 0.15f, 0.1f, 0.1f, 0.1f, 0.2f, 0.15f, 0.2f };
        PdfPTable table = null ;
        PdfPCell cell = null;






        /*打印控制*/
        for (int i = 0; i < list.Count; i++)
        {


            bean = (list[i] as MemberReceivableDetailBean);
            if (paramBean == null || !paramBean.ApartmentNo.Equals(bean.ApartmentNo))
            {
                if (paramBean == null)
                {
                    paramBean = bean;
                }

                else if (!paramBean.ApartmentNo.Equals(bean.ApartmentNo))
                {



                    //如果房间号不同或最后一条数据时，打印结束
                    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                    cell.Colspan = 7;
                    cell.Phrase = selector.Process("本期应交金额 ： " + string.Format("{0:N}", count) + "元");
                    table.AddCell(cell);




                    str = payFlg ? "元已经扣款成功，谢谢！" : "元尚未交付！";

                    cell.HorizontalAlignment = Element.ALIGN_LEFT;
                    cell.Phrase = selector.Process("您上期账单金额" + string.Format("{0:N}", lastCount) + str + "本期账单将于下个月5号统一扣款，届时请留足一卡通余额。");
                    table.AddCell(cell);
                    payFlg = false;

                   


                    //分页
                    document.Add(table);
                    document.NewPage();


                    lastCount = 0.00;
                    count = 0.00;
                    if (!paramBean.ApartmentNo.Equals(bean.ApartmentNo))
                    {
    

                        paramBean = bean;
                    }


                }
                //上期账单
                memberReceivableBean = new MemberReceivableBean();
                memberReceivableBean.ReceivableId = bean.ReceivableId;
                memberReceivableBean.ApartmentNo = bean.ApartmentNo;
                memberReceivableBean = dao.GetLastAmount(memberReceivableBean);
                if (memberReceivableBean != null)
                {
                    lastCount = Convert.ToDouble(memberReceivableBean.ReceivableAmount);
                    payFlg = memberReceivableBean.CommitFlg.Value;
                }


                table = new PdfPTable(7);
                table.WidthPercentage = 100f;
                cell = new PdfPCell(new Paragraph(" "));
                cell.Border = 0;
                cell.Colspan = 7;
                cell.BorderWidthTop = 0;


                font.Size = 10;

                cell.HorizontalAlignment = Element.ALIGN_CENTER;

                cell.Phrase = selector.Process("时期： " + date);

                table.AddCell(cell);


                cell.HorizontalAlignment = Element.ALIGN_LEFT;
                cell.Phrase = selector.Process("房间号：" + bean.BuildingNo+"号楼"+  bean.Floor.ToString()+bean.RoomNo.ToString()+"室");
                cell.BorderWidthBottom = 0.5f;
                cell.BorderColorBottom = new Color(0, 0, 0);
                table.AddCell(cell);

                ///////////////////输出数据表格开始内容



                document.Add(table);

               
                table = new PdfPTable(widths);
                table.WidthPercentage = 100f;
                cell = new PdfPCell(new Paragraph(" "));
                cell.Colspan = 7;

                font = FontFactory.GetFont("simsun", BaseFont.IDENTITY_H, 12, Font.BOLD, new Color(0, 0, 0));
                selector = new FontSelector();
                selector.AddFont(font);
                cell.Colspan = 1;

                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.Phrase = selector.Process("收费项目");
                table.AddCell(cell);

                cell.Phrase = selector.Process("本期初抄表数");
                table.AddCell(cell);

                cell.Phrase = selector.Process("本期末抄表数");
                table.AddCell(cell);

                cell.Phrase = selector.Process("本期使用量");
                table.AddCell(cell);

                cell.Phrase = selector.Process("单价");
                table.AddCell(cell);

                cell.Phrase = selector.Process("应交金额/元");
                table.AddCell(cell);

                cell.Phrase = selector.Process("备注");
                table.AddCell(cell);





                //输出第一条数据
                font = FontFactory.GetFont("simsun", BaseFont.IDENTITY_H, 10, Font.UNDEFINED, new Color(0, 0, 0));
                selector = new FontSelector();
                selector.AddFont(font);


                cell.HorizontalAlignment = Element.ALIGN_LEFT;
                cell.Phrase = selector.Process(bean.ReceivableType);
                table.AddCell(cell);

                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.Phrase = selector.Process("");
                table.AddCell(cell);

                cell.Phrase = selector.Process("");
                table.AddCell(cell);

                cell.Phrase = selector.Process("");
                table.AddCell(cell);

                cell.Phrase = selector.Process("");
                table.AddCell(cell);


                cell.Phrase = selector.Process(string.Format("{0:N}", bean.ReceivableAmount).PadLeft(7, ' '));
                table.AddCell(cell);


                cell.Phrase = selector.Process("");
                table.AddCell(cell);
                count = count + Convert.ToDouble(bean.ReceivableAmount);


                if (i + 1 >= list.Count)
                {
                    //金额合计，结束输出
                    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                    cell.Colspan = 7;
                    cell.Phrase = selector.Process("本期应交金额 ： " + string.Format("{0:N}", count)+"元");
                    table.AddCell(cell);




                    str = payFlg ? "元已经扣款成功，谢谢！" : "元尚未交付！";

                    cell.HorizontalAlignment = Element.ALIGN_LEFT;
                    cell.Phrase = selector.Process("您上期账单金额" + string.Format("{0:N}", lastCount) + str + "本期账单将于下个月5号统一扣款，届时请留足一卡通余额。");
                    table.AddCell(cell);
                    break;
                }



            }
            ///数据输出
            else
            {
                font = FontFactory.GetFont("simsun", BaseFont.IDENTITY_H, 10, Font.UNDEFINED, new Color(0, 0, 0));
                selector = new FontSelector();
                selector.AddFont(font);



                cell.HorizontalAlignment = Element.ALIGN_LEFT;
                cell.Phrase = selector.Process(bean.ReceivableType);
                table.AddCell(cell);

                cell.HorizontalAlignment = Element.ALIGN_CENTER;
                cell.Phrase = selector.Process("");
                table.AddCell(cell);

                cell.Phrase = selector.Process("");
                table.AddCell(cell);

                cell.Phrase = selector.Process("");
                table.AddCell(cell);

                cell.Phrase = selector.Process("");
                table.AddCell(cell);


                cell.Phrase = selector.Process(string.Format("{0:N}", bean.ReceivableAmount).PadLeft(7, ' '));
                table.AddCell(cell);


                cell.Phrase = selector.Process("");
                table.AddCell(cell);
                count = count + Convert.ToDouble(bean.ReceivableAmount);
                if (i + 1 >= list.Count)
                {
                    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
                    cell.Colspan = 7;
                    cell.Phrase = selector.Process("本期应交金额 ： " + string.Format("{0:N}", count) + "元");
                    table.AddCell(cell);




                    str = payFlg ? "元已经扣款成功，谢谢！" : "元尚未交付！";

                    cell.HorizontalAlignment = Element.ALIGN_LEFT;
                    cell.Phrase = selector.Process("您上期账单金额" + string.Format("{0:N}", lastCount) + str + "本期账单将于下个月5号统一扣款，届时请留足一卡通余额。");
                    table.AddCell(cell);

                    break;
                }
            }

        }

        document.Add(table);


        //数据输出



        document.Close();





        Response.AddHeader("Cache-Control", "cache");
        Response.AddHeader("Content-Type", "application/pdf");
        Response.AddHeader("Content-Length", stream.GetBuffer().Length.ToString());
        // Response.AddHeader("Content-Disposition", "attachment;filename=shpm.pdf");
        Response.BinaryWrite(stream.GetBuffer());

        Response.End();
    }
    private void set1(string date, string id)
    {
        MemoryStream stream = new MemoryStream();


        Document document = new Document(PageSize.A4, 80, 50, 30, 65);



        PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream);
        document.Open();


        FontFactory.RegisterDirectory("c:\\Windows\\Fonts");

        FontSelector selector = new FontSelector();

        Font font = FontFactory.GetFont("simsun", BaseFont.IDENTITY_H, 16, Font.BOLD, new Color(0, 0, 0));

        selector.AddFont(font);



        Paragraph title1 = new Paragraph(selector.Process("亲和源养老社区一卡通对账单"));


        title1.Alignment = Element.ALIGN_CENTER;

        document.Add(title1);








        float[] widths = { 0.15f, 0.1f, 0.1f, 0.1f, 0.2f, 0.15f, 0.2f };
        PdfPTable table = new PdfPTable(widths);
        table.WidthPercentage = 100f;
        PdfPCell cell = new PdfPCell(new Paragraph(" "));
        cell.Border = 0;
        cell.Colspan = 7;

        font.Size = 10;
        font.SetStyle(Font.UNDERLINE);
        cell.HorizontalAlignment = Element.ALIGN_CENTER;
        
        cell.Phrase = selector.Process("时期： " + date);
    
        table.AddCell(cell);

        cell.Phrase = selector.Process(" " );

        table.AddCell(cell);


       
        //数据输出
        
        AccountDao dao = new AccountDao();
        MemberReceivableBean memberReceivableBean = new MemberReceivableBean();
        MemberReceivableDetailBean bean = new MemberReceivableDetailBean();
        bean.ReceivableId = Convert.ToInt32(id);
        IList list = dao.FindAccountPrint(bean);

        double lastCount = 0.00;
        bool payFlg = false;
        double count = 0.00;
        for (int i = 0; i < list.Count; i++)
        {
            bean = (list[i] as MemberReceivableDetailBean);
            if(i==0)
            {//上期账单
                memberReceivableBean.ReceivableId=Convert.ToInt32(id);
                memberReceivableBean.ApartmentNo=bean.ApartmentNo;
                memberReceivableBean = dao.GetLastAmount(memberReceivableBean);
                if (memberReceivableBean != null)
                {
                    lastCount = Convert.ToDouble(memberReceivableBean.ReceivableAmount);
                    payFlg = memberReceivableBean.CommitFlg.Value;
                }
                //

                font.SetStyle(Font.UNDEFINED);
                cell.HorizontalAlignment = Element.ALIGN_LEFT;
                cell.Phrase = selector.Process("房间号：" + bean.BuildingNo + "号楼" + bean.Floor.ToString() + bean.RoomNo.ToString() + "室");
                cell.BorderWidthBottom = 0.5f;
                cell.BorderColorBottom = new Color(0, 0, 0);
                table.AddCell(cell);

                document.Add(table);
                //cell.BorderWidthBottom  = 1;
                //cell.Border = 1;
                
                table = new PdfPTable(widths);
                table.WidthPercentage = 100f;
                cell = new PdfPCell(new Paragraph(" "));
                cell.Colspan = 7;
                font = FontFactory.GetFont("simsun", BaseFont.IDENTITY_H, 12, Font.BOLD, new Color(0, 0, 0));
                selector = new FontSelector();
                selector.AddFont(font);
                cell.Colspan = 1;

                cell.HorizontalAlignment = Element.ALIGN_CENTER;
         
                cell.Phrase = selector.Process("收费项目");
                table.AddCell(cell);

             
                cell.Phrase = selector.Process("本期初抄表数");
                table.AddCell(cell);

           
                cell.Phrase = selector.Process("本期末抄表数");
                table.AddCell(cell);

                cell.Phrase = selector.Process("本期使用量");
                table.AddCell(cell);

                cell.Phrase = selector.Process("单价");
                table.AddCell(cell);

                cell.Phrase = selector.Process("应交金额/元");
                table.AddCell(cell);

             
                cell.Phrase = selector.Process("备注");
                table.AddCell(cell);

                font = FontFactory.GetFont("simsun", BaseFont.IDENTITY_H, 10, Font.UNDEFINED, new Color(0, 0, 0));
                selector = new FontSelector();
                selector.AddFont(font);

            }
            


            cell.HorizontalAlignment = Element.ALIGN_LEFT;
            cell.Phrase = selector.Process(bean.ReceivableType);
            table.AddCell(cell);

            cell.HorizontalAlignment = Element.ALIGN_CENTER;
            cell.Phrase = selector.Process("");
            table.AddCell(cell);

            cell.Phrase = selector.Process("");
            table.AddCell(cell);

            cell.Phrase = selector.Process("");
            table.AddCell(cell);

            cell.Phrase = selector.Process("");
            table.AddCell(cell);


            cell.Phrase = selector.Process(string.Format("{0:N}",bean.ReceivableAmount).PadLeft(7, ' '));
            table.AddCell(cell);


            cell.Phrase = selector.Process("");
            table.AddCell(cell);
            count = count + Convert.ToDouble(bean.ReceivableAmount);

        }


        //////////////////////
        cell.HorizontalAlignment = Element.ALIGN_RIGHT;
        cell.Colspan = 7;
        cell.Phrase = selector.Process("本期应交金额 ： " + string.Format("{0:N}", count)+"元");
        table.AddCell(cell);



        string str = payFlg?"元已经扣款成功，谢谢！":"元尚未交付！";
        
        cell.HorizontalAlignment = Element.ALIGN_LEFT;
        cell.Phrase = selector.Process("您上期账单金额"+string.Format("{0:N}",lastCount)+str+"本期账单将于下个月5号统一扣款，届时请留足一卡通余额。");
        table.AddCell(cell);


        document.Add(table);


        document.Close();





        Response.AddHeader("Cache-Control", "cache");
        Response.AddHeader("Content-Type", "application/pdf");
        Response.AddHeader("Content-Length", stream.GetBuffer().Length.ToString());
        // Response.AddHeader("Content-Disposition", "attachment;filename=shpm.pdf");
        Response.BinaryWrite(stream.GetBuffer());

        Response.End();
    }
}
